import { Component, OnInit, Output, EventEmitter } from '@angular/core';
import { Http } from '@angular/http';
import { InitService } from '../../../public/serve/init/init.service';
import { RemoveTableService } from '../../../public/serve/remove-table/remove-table.service';
import { HintsService } from '../../../public/serve/hints/hints.service';
import { appKeywordUrl } from '../../keyword.url';
import { keyword } from '../../keyword.data';
import { CudTableService } from '../../../public/serve/cud-table/cud-table.service';
import { DatePipe } from '@angular/common';
import { Service } from 'app/server/server';
import { FileUploader } from 'ng2-file-upload';

declare var $: any;
@Component({
  selector: 'app-trial-import',
  templateUrl: './trial-import.component.html'
})
export class TrialImportComponent implements OnInit {

  @Output() import = new EventEmitter<void>();
  uploader: FileUploader;
  hasBaseDropZoneOver: boolean;
  hasAnotherDropZoneOver: boolean;
  response: string;
  loading: boolean;

  constructor(
    private server: Service,
    public initService: InitService,
    public removeTableService: RemoveTableService,
    public hintsService: HintsService,
    private request: CudTableService,
    private datePipe: DatePipe,
    private http: Http,
  ) {
    this.uploader = new FileUploader({
      url: this.server.baseUrl + appKeywordUrl.importThemeWords,
      method: "POST",
      itemAlias: "importfile",
      disableMultipart: false
    });

    this.uploader.response.subscribe((res: any) => {
      console.log(res);
    });
  }

  ngOnInit() {
  }

  public fileOverBase(e: any): void {
    console.log(e);
    this.hasBaseDropZoneOver = e;
  }

  public fileOverAnother(e: any): void {
    console.log(e);
    this.hasAnotherDropZoneOver = e;
  }

  /**
   * 导入主题词
   */
  importUpload() {
   
    if(this.uploader.queue[0]){
      if(this.uploader.queue[0].file.type == 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' || this.uploader.queue[0].file.type == 'application/vnd.ms-excel'){
        this.loading = true;
        // 上传
        let obj = this;
        this.uploader.queue[0].onSuccess = function (response, status, headers) {
          // 上传文件成功
          if (status == 200) {
            // 上传文件后获取服务器返回的数据
            let tempRes = JSON.parse(response);
            
            obj.hintsService.messages(tempRes.message, tempRes.result_code);
            obj.import.emit();
            obj._init();
            obj.uploader.queue[0].remove();
          } else {
            // 上传文件后获取服务器返回的数据错误
            obj.hintsService.messages('上传文件失败', '9999');
          }
    
          obj.loading = false;
        };
        this.uploader.queue[0].onError = function (response, status, headers) {
          obj.hintsService.messages('上传文件失败', '9999');
          obj.loading = false;
        };
        this.uploader.queue[0].upload(); // 开始上传
      }else{
        this.hintsService.messages('请选择‘xls’或‘xlsx’格式的导入文件。', '9999');
      }
      
    }else{
      this.hintsService.messages('文件不能为空！请选择‘xls’或‘xlsx’格式的导入文件。', '9999');
    }
    
  }

  /**
    * 初始化
    */
  private _init() {
    this.initService._init();
    $("#keywordImport").modal("hide");
  }

}
